<template>
	<view class="container">
<!-- 		<u-navbar :is-fixed="false" :border-bottom="false" :is-back="true" back-icon-name="arrow-leftward"
			back-icon-color="#fff" back-icon-size="35" :background="{ background: 'linear-gradient(90deg, #69b0ff, #5f88ff)' }" title="API 请求"
			title-color="#fff">
		</u-navbar> -->
		<u-navbar title="Storage 操作" bgColor="rgba(0,0,0,0)"
		 :fixed="false"  		  	 
		 autoBack="true" >
		</u-navbar>
		<view class="u-demo">
			<view class="u-demo-wrap">
				<view class="u-demo-title">演示效果</view>
				<view class="u-demo-area u-flex u-row-center">
					<u-image width="300rpx" height="100rpx" :src="url"></u-image>
				</view>
			</view>
			<view class="u-config-wßrap">
				<view class="u-config-title u-border-bottom">参数操作</view>
				<view class="u-config-item">
					<view class="u-item-title">按钮点击</view>
					<u-button @click="doRequest">刷新验证码</u-button>
					<u-button @click="getuserinfo">获取用户信息</u-button>
					 
					
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	var WXBizDataCrypt = require('./WXBizDataCrypt')
	export default {
		data() {
			return {
				url: '',
				appid:'wxf29116035ac1a2ab',
				openid:'',
				unionid:'',
				session_key:'',
			};
		},
		methods: {
			// canIUse(){
			// 	return wx.canIUse('button.open-type.getUserInfo')
			// } ,
			doRequest() {
				let keyword = "";
				// 自动挂载至this.$u.api,无需引入
				// captcha 在 /api/demo.js 内定义
				this.$u.api.captcha().then(data => {
					console.log(data)
					this.url = data.image;
					this.$u.func.showToast({
						title: '数据获取成功',
					})
				}).catch(() => {

				})
			},
			getuserinfo(){
				console.log(uni.$u.config.v);
				const that = this;
				wx.login({  
				        success: (res) => {  
				            if (res.code) {         //微信登录成功 已拿到code  
				                // ...doSomething   
								 wx.request({
								             url: 'https://www.xrskfw.com/api/xrsk-server/home/wxUserAccess',
								             data: {
								               code: res.code
								             },  
								             method: 'POST',
								             dataType: 'json',
								             success: function (cts) {
								                //请求成功的处理											 
												console.log(cts)    
												that.session_key= cts.data.session_key;
												that.openid = cts.data.openid;
													
								             }})
				            } else {  
				                console.log('登录失败！' + res.errMsg)  
				            } 
							 
							 
							 
				        }  
				    }) 								
			},
			getPhoneNumber(res){ // 获取手机号
				console.log(this.session_key)	
				console.log(res)	
				
				var appId = 'wxf29116035ac1a2ab'
				var sessionKey = this.session_key;
				var encryptedData = res.detail.encryptedData;			 
				var iv = res.detail.iv;
				var pc = new WXBizDataCrypt(appId, sessionKey)				
				var data = pc.decryptData(encryptedData , iv)
				
				console.log(data)	
			}, 
			bindGetUserInfo(){		 
				console.log(uni.$u.config.v);
			}
			// getPhoneNumber(){
			// 	uni
			//         if(e.detail.errMsg=="getPhoneNumber:fail user deny"){       //用户拒绝授权  
			// 			  console.log('用户拒绝授权')  
			//             //拒绝授权后弹出一些提示  
			
			//         }else{      //允许授权  
			//             console.log(e.detail.encryptedData)  
			//             e.detail.encryptedData      //加密的用户信息  
			//             e.detail.iv     //加密算法的初始向量  时要用到  
			//         }  
			//     }  
		},
	}
</script>
